﻿using MvcSimpleQuery.QueryTree;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace SimpleQueryExample.Controllers
{
    public class SimpleQueryController : Controller
    {
        //
        // GET: /SimpleQuery/
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Customers(List<Conditions> queries)
        { 

            var nwe = new northwindEntities();
            IQueryable<Customer> queryableData = from item in nwe.Customers select item;

            Expression whereClause; 

            whereClause = queries.CreateExpression<Customer>( queryableData);
            var filtered = from item in queryableData.Provider.CreateQuery<Customer>(whereClause)
                           select new
                           {
                               CustomerID = item.CustomerID
                               ,
                               CompanyName = item.CompanyName
                               ,
                               ContactName = item.ContactName
                               ,
                               ContactTitle = item.ContactTitle
                               ,
                               Address = item.Address
                               ,
                               City = item.City
                               ,
                               Region = item.Region
                               ,
                               PostalCode = item.PostalCode
                               ,
                               Country = item.Country
                               ,
                               Phone = item.Phone
                               ,
                               Fax = item.Fax
                           };

            return Json(filtered, JsonRequestBehavior.AllowGet);

        }

        

    }
}